package com.kunbo.cn.util.common;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import com.kunbo.cn.entity.uflo.entity.UploadLog;



public class uploadUtil {
	@Value("${file.upload.dir}")
    private static String path;
	
	private final static Logger log = Logger.getLogger(uploadUtil.class.getName());
	//单个文件上传
	public static UploadLog upload(MultipartFile file,String path,String sidePath) {
		UploadLog up = new UploadLog();
		if(file.isEmpty()) {
			return up;
		}
		String fileName = file.getOriginalFilename();
		if(fileName.equals("")) {
			// 如果没有传入自定义文件名，则自动获取文件名称
	        fileName = file.getOriginalFilename();
		}
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 文件上传后的路径
        // 解决中文问题，liunx下中文路径，图片显示问题
        fileName = UUID.randomUUID() + fileName;
        File dest = new File(path + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        System.out.println("调用路径为"+sidePath + fileName);
        up.setFileName(file.getOriginalFilename());
        up.setFileType(suffixName);
        up.setFilePath(sidePath + fileName);
        up.setFileRealPath(path + fileName);
        return up;
    }
	
    public static Map<String,Object> downLoadFile(File file,String fileName,HttpServletResponse response) throws UnsupportedEncodingException{
    	if(file.exists()){ //判断文件父目录是否存在
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
           // response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=" +   java.net.URLEncoder.encode(fileName,"UTF-8"));
            byte[] buffer = new byte[1024];
            FileInputStream fis = null; //文件输入流
            BufferedInputStream bis = null;

            OutputStream os = null; //输出流
            try {
                os = response.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while(i != -1){
                    os.write(buffer);
                    i = bis.read(buffer);
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("----------file download---" + fileName);
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
		return null;
    	
    }
}
